import {onMounted, reactive} from 'vue';
import router from '@/router';
import { sliceTime } from '@/utils/time';
import {querySfCte, getExcel, querySfCteS, detailPB} from "@api/url/erp/baseMessage"
import { MOCTECODE } from "@/utils/yfCode"
import { hiprint, defaultElementTypeProvider, print } from "vue-plugin-hiprint";
import printMode from './printMode.json'
import notification from "@arco-design/web-vue/es/notification";
import {sfcteGetExcel} from "@api/url/base";
export default function importConfig() {
  const base = reactive<any>({
    data: [],
    sendWorkTabel: [],
    printData: {},
    printVisable: false,
    param: "",
    title:'',
  });

  // 导出Excle
  const outExcleFun = () => {
    // router.push({
    //     path: 'api/sfcte/getExcel',
    //     query: { single: base.param.single.trim(), odd: base.param.odd.trim(), gx: base.param.gx.trim() },
    // });
    // downloadFile(location.origin + `/api/sfcte/getExcel?single=${base.param.single}&odd=${base.param.odd}&gx=${base.param.gx}`);
      let obj={
          single:base.param.single,
          odd:base.param.odd,
          gx:base.param.gx
      };
      sfcteGetExcel(obj).then(res=>{
          if(res.code==201){
              notification.info(res.data);
          }else{
              downloadFile(import.meta.env.VITE_APP_API_BASE_URL + `/sfcte/getExcel?single=${base.param.single}&odd=${base.param.odd}&gx=${base.param.gx}`);
          }
      });
  };

  // 下载
  const downloadFile=(url)=>{
      const link = document.createElement('a');
      link.style.display = 'none';
      link.href = url;
      link.download = url.substring(url.lastIndexOf('/') + 1, url.length);
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
  }

  //获取页面数据
  const getData=()=>{
      base.param = router.currentRoute.value.query;
      if(base.param.source=='1'){
          let obj={
              single:router.currentRoute.value.query.single,
              odd:router.currentRoute.value.query.odd,
              gx:router.currentRoute.value.query.gx,
          };

          base.title='工单工艺的报工单';
          querySfCte(obj).then((res: any) => {
              console.log(res);
              if(res.code == '200'){
                  base.sendWorkTabel = [];
                  base.data = res.data.map((item: any) => {
                      item.CREATE_DATE = sliceTime(item.CREATE_DATE);
                      console.log(MOCTECODE(item));
                      base.sendWorkTabel.push(MOCTECODE(item))
                  });


                  base.data = res.data.map((item: any) => {
                      return JSON.stringify(item);
                  });
              }
          });
      }else if(base.param.source=='2'){
          let obj={
              single:router.currentRoute.value.query.single,
              odd:router.currentRoute.value.query.odd,
              gx:router.currentRoute.value.query.gx,
          };

          base.title='工单工艺的派工单';
          querySfCteS(obj).then((res: any) => {
              if (res.code == '200') {
                  base.sendWorkTabel = [];
                  base.data = res.data.map((item: any) => {
                      item.CREATE_DATE = sliceTime(item.CREATE_DATE);
                      console.log(MOCTECODE(item));
                      base.sendWorkTabel.push(MOCTECODE(item))
                  })
                  base.data = res.data.map((item: any) => {
                      return JSON.stringify(item);
                  });
              }
          });
      }else{
          let obj={
              TE001:router.currentRoute.value.query.single,
              TE002:router.currentRoute.value.query.odd,
          };

          if(base.param.source=='3'){
              base.title='报工查询详情';
          }else if(base.param.source=='4'){
              base.title='派工查询详情';
              base.printVisable = true;
          }

          detailPB(obj).then((res: any) => {
              if (res.code == '200') {
                  base.sendWorkTabel = [];
                  base.printData = res.data;
                  base.printData.qrcode = base.printData.TE001 + "-" + base.printData.TE002 + "-" + base.printData.TE003;
                  Object.keys(base.printData).forEach(key=>{
                      if(key=='sonList'){
                          if(base.printData.sonList.length>0){
                              base.printData.sonList.forEach(item=>{
                                  item.TE004=base.printData.TE004;
                                  item.UDF07=base.printData.UDF07;
                              });
                          }
                      }
                  });
                  if(res.data){
                      res.data.CREATE_DATE=sliceTime(res.data?.CREATE_DATE);
                      base.sendWorkTabel.push(MOCTECODE(res.data));
                  }
                  base.data =res.data;
              }else{
                  notification.info(res.message);
              }
          });
      }
  };

  const printFun = () => {
    // console.log(panelMode)
      console.log(base.printData);
    let hiprintTemplatePrint = new hiprint.PrintTemplate({ template: printMode});
    hiprintTemplatePrint.print(base.printData);
  }

  onMounted(()=>{
      getData();
  });

  return {
    base,
    outExcleFun,
    printFun,
  }
}
